/**
 * Project Name:mauto-web
 * File Name:ApiEnterpriseController.java
 * Package Name:com.inftech.mauto.sys.web.front
 * Date:2016年10月17日下午9:51:38
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.core.web.front;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.inftech.mauto.common.base.BaseController;
import com.inftech.mauto.common.base.BusinessException;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.common.constant.SessionConstEnum;
import com.inftech.mauto.core.entity.Order;
import com.inftech.mauto.core.service.OrderService;
import com.inftech.mauto.jy.entity.JyOrder;
import com.inftech.mauto.jy.service.JyOrderService;
import com.inftech.mauto.sys.service.CarBasicService;
import com.inftech.mauto.sys.service.IdentifierService;
import com.mingsoft.people.dao.IPeopleDao;
import com.mingsoft.people.entity.PeopleEntity;
import com.mingsoft.people.entity.PeopleUserEntity;
import com.mingsoft.weixin.entity.WeixinPeopleEntity;

/**
 * ClassName:ApiOrderController <br/>
 * Function: 订单接口. <br/>
 * Date: 2016年10月17日 下午9:51:38 <br/>
 * 
 * @author jeno
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/api/order")
public class ApiOrderController extends BaseController {

	/**
	 * 注入订单业务层
	 */
	@Autowired
	protected OrderService orderService;

	@Resource
	private JyOrderService jyOrderServiceImpl;

	@Resource(name = "identifierService")
	private IdentifierService identifierServiceImpl;

	@Resource(name = "carBasicService")
	private CarBasicService carBasicService;

	@Autowired
	private IPeopleDao peopleDao;

	/**
	 * 
	 * list:获取订单列表. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/list/{orderStatus}", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String list(@PathVariable int orderStatus, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("orderStatus", orderStatus);
			// 获取订单列表
			List<Map<String, Object>> orderList = orderService.queryOrderListFront(paramMap, people);
			LOG.info("获取订单列表成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), orderList);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("获取订单列表失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}
	
	/**
	 * 
	 * allList:微信端 我的订单. <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/allList", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String allList(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			String orderStatus = "";
			String jyorderStatus = "";
			String status = request.getParameter("status");
			String type = request.getParameter("type");
			int pageSize = Integer.parseInt(request.getParameter("pageSize") == null ? "10" : request.getParameter("pageSize").toString());
			int pageNo = Integer.parseInt(request.getParameter("pageNo") == null ? "1" : request.getParameter("pageNo").toString());
			System.out.println("status=>"+status);
			System.out.println("type=>"+type);
			System.out.println("pageSize=>"+pageSize);
			System.out.println("pageNo=>"+pageNo);
			//All,DCL,DWX,SPJ
			if("ALL".equals(status)) {
				orderStatus = "0";
				jyorderStatus = "";
			} else if("DCL".equals(status)) {
				orderStatus = "20";
				jyorderStatus = "0";			
			}else if("DWX".equals(status)) {
				orderStatus = "3";
				jyorderStatus = "1";		
			}else if("DPJ".equals(status)) {
				orderStatus = "4";
				jyorderStatus = "4";	
			}
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("orderStatus", orderStatus);
			paramMap.put("type", type);
			paramMap.put("jyorderStatus", jyorderStatus);
			paramMap.put("begin", (pageNo - 1) * pageSize);
			paramMap.put("rows", pageSize);
			// 获取订单列表
			Map<String, Object> orderList = orderService.queryAllOrderList(paramMap, people);
			LOG.info("获取订单列表成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), orderList);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("获取订单列表失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * detail:获取订单详情. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/detail/{orderCode}", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String detail(@PathVariable String orderCode, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("order_code", orderCode);
			Map<String, Object> orderMap = orderService.queryOrderFront(paramMap, people);
			LOG.info("获取订单详情成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), orderMap);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("获取订单详情失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * count:获取订单数量. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/count", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String count(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> countMap = orderService.queryOrderCount(people);
			LOG.info("获取订单数量成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), countMap);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("获取订单数量失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * save:新增订单. <br/>
	 *
	 * @author puyf
	 * @param order
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String save(@RequestBody Order order, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			orderService.saveOrder(order, people);
			LOG.info("新增订单成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("新增订单失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * cancel:取消订单. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/cancel", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String cancel(@RequestBody String requestStr, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			JSONObject jsonObject = (JSONObject) JSONObject.parse(requestStr);
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("orderCode", jsonObject.getString("orderCode"));
			paramMap.put("orderCancelReason", jsonObject.getString("orderCancelReason"));
			orderService.modifyOrderCancel(paramMap, people, true);
			LOG.info("取消订单成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("取消订单失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * cancel:确认订单. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/confirm", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String confirm(@RequestBody String requestStr, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			JSONObject jsonObject = (JSONObject) JSONObject.parse(requestStr);
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("orderCode", jsonObject.getString("orderCode"));
			paramMap.put("orderAppointTime", jsonObject.getString("orderAppointTime"));
			orderService.modifyOrderConfirm(paramMap, people);
			LOG.info("确认订单成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("确认订单失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * modify:修改订单预约时间. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/modify", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String modify(@RequestBody String requestStr, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			JSONObject jsonObject = (JSONObject) JSONObject.parse(requestStr);
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("orderCode", jsonObject.getString("orderCode"));
			paramMap.put("orderAppointTime", jsonObject.getString("orderAppointTime"));
			orderService.modifyOrderAppontmentTime(paramMap, people);
			LOG.info("修改订单预约时间成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("修改订单预约时间失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * sign:签收订单. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/sign", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String sign(@RequestBody String requestStr, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			JSONObject jsonObject = (JSONObject) JSONObject.parse(requestStr);
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("orderCode", jsonObject.getString("orderCode"));
			// paramMap.put("orderSignTime",
			// jsonObject.getString("orderSignTime"));
			orderService.modifyOrderSign(paramMap, people);
			LOG.info("签收订单成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("签收订单失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	@RequestMapping(value = "/jy/getWxItems", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getWxItems(String planType, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("peopleId", people.getPeopleId());
			// planType 1 是高端 2 是终端 3是低端
			paramMap.put("planType", planType);
			// 获取配件列表
			List<Map<String, Object>> pjList = orderService.getWxItems(paramMap);
			// 所有套餐列表
			List<Map<String, Object>> tcList = jyOrderServiceImpl.getTcList();
			// 所有项目列表
			List<Map<String, Object>> xmlList = jyOrderServiceImpl.getXmList();
			for (Map<String, Object> xmlListMap : xmlList) {
				String pjlb = xmlListMap.get("pjlb").toString();
				List<Map<String, Object>> pjxxList = jyOrderServiceImpl.getPJXXByPJLB(pjlb);
				if (pjxxList.size() != 0) {
					xmlListMap.put("Defaultpj", pjxxList.get(0));
				}
			}

			Map<String, List<Map<String, Object>>> map = new HashMap<String, List<Map<String, Object>>>();
			for (Map<String, Object> tcMap : tcList) {
				// 套餐id
				Object id = tcMap.get("id");
				List<Map<String, Object>> idList = map.get(id);
				if (idList == null) {
					idList = new ArrayList<Map<String, Object>>();
				}
				Map<String, Object> cunMap = new HashMap<String, Object>();
				cunMap.put("xmName", tcMap.get("xmName"));
				cunMap.put("xmprice", tcMap.get("xmprice"));
				cunMap.put("pjlb", tcMap.get("pjlb"));
				cunMap.put("isZd", tcMap.get("isZd"));
				idList.add(cunMap);
				if (idList.size() == 1) {
					map.put(id.toString(), idList);
				}
			}
			// 存放 tcList
			List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();

			for (Map<String, Object> tcMap : tcList) {
				Map<String, Object> map1 = new HashMap<String, Object>();
				Object id = tcMap.get("id");
				Object id1 = map1.get(id);
				if (id1 == null) {
					List<Map<String, Object>> idList = map.get(id);
					// 根据配件类别 获取对应配件列表第一个配件的信息
					for (Map<String, Object> idListMap : idList) {
						String pjlb = idListMap.get("pjlb").toString();
						List<Map<String, Object>> pjxxList = jyOrderServiceImpl.getPJXXByPJLB(pjlb);
						if (pjxxList.size() != 0) {
							idListMap.put("Defaultpj", pjxxList.get(0));
						}
					}
					map1.put(id.toString(), id);
					map1.put("tcName", tcMap.get("tcName"));
					map1.put("tcJmfy", tcMap.get("tcJmfy"));
					map1.put("tcBzdjm", tcMap.get("tcBzdjm"));
					map1.put("tcxmList", idList);
					mapList.add(map1);
				}
			}
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("tcList", mapList);
			resultMap.put("xmList", xmlList);
			resultMap.put("pjList", pjList);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), resultMap);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			// LOG.error("签收订单失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * getFwItems:(获取服务费用). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/jy/getFwItems", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getFwItems(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			String gsf = jyOrderServiceImpl.getWxxmPrice("工时费");
			String smfwf = jyOrderServiceImpl.getWxxmPrice("上门服务费");
			Map<String, Object> result = new HashMap<String, Object>();
			List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
			result.put("xmName", "工时费");
			result.put("xmfy", gsf);
			resultList.add(result);
			Map<String, Object> result2 = new HashMap<String, Object>();
			result2.put("xmName", "上门服务费");
			result2.put("xmfy", smfwf);
			resultList.add(result2);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), resultList);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			// LOG.error("签收订单失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * save:(保存订单). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/jy/save", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String save(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);

			// 人员信息
			PeopleEntity peopleEntity = peopleDao.getContactDetailByPeopleId(people.getPeopleId());
			PeopleUserEntity peopleUserEntity = peopleEntity.getPeopleUser();
			// 车辆信息
			Map<String, Object> mapCar = carBasicService.getCarDetail(people.getPeopleId());
			mapCar.put("contactName", peopleUserEntity.getPeopleUserRealName());
			if (peopleUserEntity.getPeopleUserSex() == 1) {
				mapCar.put("contactSex", "男");
			}
			if (peopleUserEntity.getPeopleUserSex() == 2) {
				mapCar.put("contactSex", "女");
			}
			if (peopleUserEntity.getPeopleUserSex() == 0) {
				mapCar.put("contactSex", "未知");
			}
			mapCar.put("contactPhone", peopleEntity.getPeoplePhone());
			mapCar.put("contactAdress", peopleUserEntity.getPeopleUserAddress());
			mapCar.put("orderId", "");
			String orderCode = identifierServiceImpl.readIdentifierBySeparator("BY");
			String jsonStr = request.getParameter("jsonStr");
			JSONObject jsonObject = JSONObject.parseObject(jsonStr);
			List<Map> tcList = JSONArray.parseArray(JSONObject.toJSONString(jsonObject.get("tcList")), Map.class);
			List<Map> xmList = JSONArray.parseArray(JSONObject.toJSONString(jsonObject.get("xmList")), Map.class);
			String fzdjmf = jsonObject.getString("fzdjmf");
			String hj = jsonObject.getString("hj");
			mapCar.put("fzdjmf", fzdjmf);
			mapCar.put("hj", hj);
			mapCar.put("orderCode", orderCode);
			mapCar.put("people_id", people.getPeopleId());
			// 插入订单信息
			jyOrderServiceImpl.insertOrder(mapCar);
			for (Map<Object, Object> tcMap : tcList) {
				tcMap.put("tcId", "");
				tcMap.put("orderId", mapCar.get("mapCar"));
				jyOrderServiceImpl.insertJyOrderTc(tcMap);
				for (Map map1 : JSONArray.parseArray(JSONObject.toJSONString(tcMap.get("tcxmList")), Map.class)) {
					map1.put("tcId", tcMap.get("tcId"));
					map1.put("wxxmid", "");
					map1.put("orderId", mapCar.get("mapCar"));
					jyOrderServiceImpl.insertJyOrderWxxmWithTcId(map1);
					jyOrderServiceImpl.insertJyOrderPj(map1);
				}
			}
			for (Map<Object, Object> xmlMap : xmList) {
				xmlMap.put("wxxmid", "");
				xmlMap.put("orderId", mapCar.get("mapCar"));
				jyOrderServiceImpl.insertJyOrderWxxm(xmlMap);
				jyOrderServiceImpl.insertJyOrderPj(xmlMap);
			}
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);

		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * detail:(订单详情). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/jy/detail", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String detail(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {

			String orderCode = request.getParameter("orderCode");
			JyOrder jyOrder = jyOrderServiceImpl.getOrderByOrderCode(orderCode);
			List<Map<String, Object>> tcList = jyOrderServiceImpl.getTcListByOrderCode(orderCode);
			List<Map<String, Object>> xmlList = jyOrderServiceImpl.getXmListByOrderCode(orderCode);
			Map<String, Object> resultMap = new HashMap<String, Object>();
			List<String> list = new ArrayList<String>();

			// 求出所有的套餐id
			for (Map<String, Object> tcMap : tcList) {
				if (!list.contains(tcMap.get("id").toString())) {
					list.add(tcMap.get("id").toString());
				}
			}

			// 通过id进行判断
			List<Map<String, Object>> resultTcList = new ArrayList<Map<String, Object>>();
			for (String id : list) {
				Map<String, Object> cunMap = new HashMap<String, Object>();
				List<Map<String, Object>> tcxmList = new ArrayList<Map<String, Object>>();
				for (Map<String, Object> tcMap : tcList) {
					if (id.equals(tcMap.get("id").toString())) {
						Map<String, Object> tcxmMap = new HashMap<String, Object>();
						cunMap.put("tcName", tcMap.get("tcName"));
						cunMap.put("tcJmfy", tcMap.get("tcJmfy"));
						cunMap.put("tcPrice", tcMap.get("tcPrice"));
						tcxmMap.put("xmName", tcMap.get("xmName"));
						tcxmMap.put("xmprice", tcMap.get("xmprice"));
						tcxmMap.put("pjlb", tcMap.get("pjlb"));
						tcxmMap.put("pjCode", tcMap.get("pjCode"));
						tcxmMap.put("pjpp", tcMap.get("pjpp"));
						tcxmMap.put("pjPrice", tcMap.get("pjPrice"));
						tcxmMap.put("pjNum", tcMap.get("pjNum"));
						tcxmMap.put("isZd", tcMap.get("isZd"));
						tcxmList.add(tcxmMap);
					}
				}
				cunMap.put("tcxmList", tcxmList);
				resultTcList.add(cunMap);
			}

			/**
			 * orderId": "XXXX", "orderCode": "XXXX", "carXh": "车辆型号", " carDate
			 * ": " 2017-01-01 09:00", "": "行驶里程", "": "订单状态", "": "订单金额", "":
			 * "联系人姓名", "": "联系人性别", "": "联系人手机", "": "联系人地址", "": "创建时间", "":
			 * "派单时间", "": "维养开始时间", "": "维养结束时间", "": "支付时间", "": "关闭时间", "":
			 * "取消时间"
			 */
			resultMap.put("orderId", jyOrder.getId());
			resultMap.put("orderCode", jyOrder.getOrderCode());
			resultMap.put("carXh", jyOrder.getCarXh());
			resultMap.put("carDate", jyOrder.getCarDate());
			resultMap.put("carXslc", jyOrder.getCarXslc());
			resultMap.put("orderStatus", jyOrder.getOrderStatus());
			resultMap.put("orderSum", jyOrder.getOrderPrice());
			resultMap.put("contactName", jyOrder.getContactPerson());
			resultMap.put("contactSex", jyOrder.getContactSex());
			resultMap.put("contactPhone", jyOrder.getContactPhone());
			resultMap.put("contactAdress", jyOrder.getContactAddress());
			resultMap.put("createTime", jyOrder.getCreateTime());
			resultMap.put("pdTime", jyOrder.getPdTime());
			resultMap.put("startTime", jyOrder.getWyStartTime());
			resultMap.put("endTime", jyOrder.getWyEndTime());
			resultMap.put("payTime", jyOrder.getPayTime());
			resultMap.put("closeTime", jyOrder.getCloseTime());
			resultMap.put("cancelTime", jyOrder.getCancelTime());

			resultMap.put("tcList", resultTcList);
			resultMap.put("xmList", xmlList);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), resultMap);

		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * cancel:(取消订单). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/jy/cancel", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String cancel(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {

			String orderCode = request.getParameter("orderCode");
			String cancelReason = request.getParameter("cancelReason");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("orderCode", orderCode);
			map.put("cancelReason", cancelReason);
			int row = jyOrderServiceImpl.updateCancelByOrderCode(map);
			if (row == 1) {
				HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
				outJson = getOutJson(statusCode, true, statusCode.toString(), null);
				return outJson;
			}
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * jcbgDetail:(加载检测报告). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/jy/jcbgDetail", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String jcbgDetail(HttpServletRequest request) throws Exception {
		String orderCode = request.getParameter("orderCode");
		// 检测报告列表
		List<Map<String, Object>> detailList = jyOrderServiceImpl.getJcbgDetail(orderCode);
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		Set<String> set = new LinkedHashSet<String>();
		// 获取所有的类型id
		for (Map<String, Object> map : detailList) {
			set.add(map.get("id").toString());
		}
		// 根据类型id获取对应的list
		for (String str : set) {
			// 存放数据的map
			Map<String, Object> map1 = new HashMap<>();
			// 存放的list
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			// 收集list
			for (Map<String, Object> map : detailList) {
				if (str.equals(map.get("id").toString())) {
					map1.put("typeName", map.get("typeName"));
					// list里面的对象
					Map<String, Object> map2 = new HashMap<String, Object>();
					map2.put("bgxmName", map.get("bgxmName"));
					map2.put("bgxmValue", map.get("bgxmValue"));
					// 放在list中
					list.add(map2);
				}
			}
			// 将list 放在map中
			map1.put("list", list);
			// 将存放数据的map放在返回的list中
			resultList.add(map1);
		}
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		String outJson = getOutJson(statusCode, true, statusCode.toString(), resultList);
		return outJson;
	}

}
